﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AI_Tank.Game
{
    class ActionObj
    {
        private int teamId;
        private int playerId;
        private Direction? moveDirection;
        private Direction? fireDirection;
        private BulletType bulletType;

        public ActionObj(int id,int playerId, Direction? mov, Direction? fire,BulletType bulType)
        {
            this.teamId = id;
            this.playerId = playerId;
            this.moveDirection = mov;
            this.fireDirection = fire;
            this.bulletType = bulType;
        }

        public int TeamId { get => teamId;  }
        public int PlayerId { get => playerId; }
        public Direction? MoveDirection { get => moveDirection;}
        public Direction? FireDirection { get => fireDirection; }
        public BulletType BulletType { get => bulletType;  }
    }

    class ActionData
    {
        private int roundId;
        private List<ActionObj> actions = new List<ActionObj>();
        public ActionData(int roundId,List<ActionObj> actions)
        {
            this.roundId = roundId;
            for(int i=0;i<actions.Count;i++)
            {
                ActionObj obj = actions[i];
                actions.Add(obj);
            }
        }

        public string GetActionDataString()
        {
            JObject obj = new JObject();
            JObject msgData = new JObject();
            obj.Add("msg_name", "action");
            msgData.Add("round_id", roundId);
            JArray actionsArr = new JArray();
            for(int i=0;i< actions.Count; i++)
            {
                JObject actionObj = new JObject();
                actionObj.Add("team", actions[i].TeamId);
                actionObj.Add("player_id", actions[i].PlayerId);
                JArray strMovArr = new JArray();
                JArray strfireArr = new JArray();
                string str;
                switch (actions[i].MoveDirection)
                {
                    
                    case Direction.Up:str = "up"; strMovArr.Add(str); actionObj.Add("move", strMovArr);break;
                    case Direction.Down: str = "down"; strMovArr.Add(str); actionObj.Add("move", strMovArr); break;
                    case Direction.Left: str = "left"; strMovArr.Add(str); actionObj.Add("move", strMovArr); break;
                    case Direction.Right: str = "right"; strMovArr.Add(str); actionObj.Add("move", strMovArr); break;
                    default: actionObj.Add("move", strMovArr);break;
                }
                switch (actions[i].FireDirection)
                {
                    case Direction.Up: str = "up"; strfireArr.Add(str); actionObj.Add("fire", strfireArr); break;
                    case Direction.Down: str = "down"; strfireArr.Add(str); actionObj.Add("fire", strfireArr); break;
                    case Direction.Left: str = "left"; strfireArr.Add(str); actionObj.Add("fire", strfireArr); break;
                    case Direction.Right: str = "right"; strfireArr.Add(str); actionObj.Add("fire", strfireArr); break;
                    default: actionObj.Add("move", strfireArr); break;
                }
                int bulletType;
                if (actions[i].BulletType == BulletType.Super)
                    bulletType = 1;
                else
                    bulletType = 0;
                actionObj.Add("bullet_type", bulletType);
                actionsArr.Add(actionObj);
            }
            msgData.Add("actions", actionsArr);
            obj.Add("msg_data", msgData);
            return obj.ToString();
        }
    }
}
